package cn.turing.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import cn.turing.model.Student;

public class MainFrame extends JFrame
{
	public static JTable t;
	
	public MainFrame()
	{
		setSize(800, 600);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle("学生信息管理系统");
		init();
		setVisible(true);
	}
	
	public void init()
	{
		JMenuBar menuBar = new JMenuBar();
		
		JMenu fileMenu = new JMenu("文件");
		JMenu editMenu = new JMenu("编辑");
		JMenu helpMenu = new JMenu("帮助");

		JMenuItem newMenuItem=new JMenuItem("打开",new ImageIcon("images/new.png"));
		JMenuItem saveMenuItem=new JMenuItem("保存",new ImageIcon("images/btn-save.png"));
		JMenuItem exitMenuItem=new JMenuItem("退出",new ImageIcon("images/exit.png"));
		fileMenu.add(newMenuItem);
		fileMenu.add(saveMenuItem);
		fileMenu.add(exitMenuItem);
		
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(helpMenu);
		setJMenuBar(menuBar);
		
		/**==================按钮=====================**/
		JToolBar toolBar=new JToolBar();
		JButton addButton = new JButton("添加",new ImageIcon("images/add.png"));
		JButton editButton = new JButton("编辑",new ImageIcon("images/edit.png"));
		JButton delButton = new JButton("删除",new ImageIcon("images/delete.png"));
		JButton exitButton = new JButton("退出",new ImageIcon("images/exit.png"));
		
		toolBar.add(addButton);
		toolBar.add(editButton);
		toolBar.add(delButton);
		toolBar.add(exitButton);
		
		add(toolBar,BorderLayout.NORTH);
		
		/**======================表格=====================**/
		StudentTableModel st=new StudentTableModel();
		t=new JTable(st);
		JScrollPane spanel=new JScrollPane(t); 
		
		/**======================事件监听==========================**/
		//添加
		addButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				StudentAddFrame s=new StudentAddFrame();
				
			}
		});
		//编辑
		editButton.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent e)
			{
				int selectedRow=t.getSelectedRow();
				StudentTableModel stm=new StudentTableModel();
                Student student=new Student();
                
                stm.setValueAt(student, selectedRow, 0);
                stm.setValueAt(student, selectedRow, 1);
                stm.setValueAt(student, selectedRow, 2);
                stm.setValueAt(student, selectedRow, 3);
                stm.setValueAt(student, selectedRow, 4);
                
				StudentEditFrame s=new StudentEditFrame(student);
				
			}
		});
		
		
		add(spanel,BorderLayout.CENTER);
	}
}
